
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <signal.h>

#include "xinit.h"
#include "fractal.h"
#include "sincos.h"
#include "bump.h"
#include "types.h"
#include "timer.h"

int InfiniteLoop=TRUE;

XWindow *xw;

void
OnBreak(int Arg)
{
  InfiniteLoop=FALSE;
}

void
ClearKBuf()
{
  XEvent ev;
  long KEY;

  while(XCheckWindowEvent(xw->display,xw->window,KeyPressMask,&ev))
    {
      KEY=XLookupKeysym((XKeyEvent *)&ev,0);  
    }
}

int
main(int argc,char *argv[])
{
  int m,n;
  short *s;
  int r,g,b;

  xw=(XWindow *)malloc(sizeof(XWindow));
  if(OpenXWindow(xw,320,200,24,"_%(2D Bump)%_",NULL,TRUE,TRUE,TRUE,TRUE))
    {
      printf("\nClosing application.\n");
      return 1;
    }

  signal(SIGHUP,OnBreak);
  signal(SIGINT,OnBreak);
  signal(SIGQUIT,OnBreak);
  signal(SIGTERM,OnBreak);

  CreateMap();

  BumpMap=(char *)malloc(xw->width*xw->height*sizeof(char));
  for(n=0;n<200;n++)
    memcpy((char *)&BumpMap[n*320],(char *)&colormap[n*256],256);
  for(n=0;n<200;n++)
    for(m=0;m<(320-256);m++)
      BumpMap[n*320+256+m]=colormap[n*256+m];

  memset(xw->virtualscreen,0,xw->screensize);

  MakeSinCos(160,100);

  init_timer(0,20000);
  TimerCallFunc=IncBump;

  while(InfiniteLoop)
    {
      XEvent ev;
      long KEY;

      while(XPending(xw->display))
	{
	  XNextEvent(xw->display,&ev);
	  switch(ev.type)
	    {
	    case KeyPress:
	      KEY=XLookupKeysym((XKeyEvent *)&ev,0);
	      switch(KEY)
		{
		case XK_Escape:
		  InfiniteLoop=FALSE;
		  break;
		}
	      ClearKBuf();
	      break;
	    case ButtonPress:
	      InfiniteLoop=FALSE;
	      break;
	    }
	}
      Bump(xw,TimerCount);
      PutXImage(xw);
    }
  TrashXWindow(xw);
  clear_timer();
  return 0;
}
